import { createRouter, createWebHistory } from 'vue-router'
import layout from '../views/layout/index.vue'
import Cookies from 'js-cookie'
import { Help as IconHelp } from '@element-plus/icons-vue';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: { hidden: true }
    },

    {
      path: '/',
      name: 'home',
      component: layout,
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('../home/index.vue'),
          meta: { icon: "home", title: "首页" }
        },
        {
          path: '/profile',
          component: () => import('../views/user/profile.vue'),
          meta: { hidden: true, title: "个人信息" }
        },
      ]
    },
    {
      path: '/user',
      component: layout,
      redirect: '/student',
      meta: { icon: "用户", title: "用户管理" }, // 用户图标
      children: [
        {
          path: '/student',
          component: () => import('../views/user/student.vue'),
          meta: {  title: "学生管理" }
        },
        {
          path: '/studentAdd',
          component: () => import('../views/user/StudentAdd.vue'),
          meta: { title: "新增学生", hidden: true }
        },
        {
          path: '/teacher',
          component: () => import('../views/user/teacher.vue'),
          meta: { title: "教师管理" }
        },
        {
          path: '/teacherAdd',
          component: () => import('../views/user/teacherAdd.vue'),
          meta: { title: "新增管理", hidden: true }
        }
      ]
    },
    {
      path: '/07-chengji',
      component: layout,
      redirect: '/07-chengji/dajuan',
      name: '成绩管理',
      meta: { alwaysShow: true, title: '成绩管理', },
      children: [
        {
          path: '/dajuan',
          component: () => import('../views/07-chengji/dajuan.vue'),
          name: 'dajuan',
          meta: {   icon: 'chengji',title: '答卷列表' }
        }
      ]
    },
    {
      path: '/08-xiaoxi',
      component: layout,
      redirect: '/08-xiaoxi/liebiao',
      name: '消息中心',
      meta: { alwaysShow: true, title: '消息中心', icon: 'xiaoxi' },
      children: [
        {
          path: '/liebiao',
          component: () => import('../views/08-xiaoxi/liebiao.vue'),
          name: 'liebiao',
          meta: { title: '消息列表' }
        },
        {
          path: '/fasong',
          component: () => import('../views/08-xiaoxi/fasong.vue'),
          name: 'fasong',
          meta: { title: '消息发送' }
        }
      ]
    },
    {
      path: '/09-rizhi',
      component: layout,
      redirect: '/09-rizhi/rizhi',
      name: '日志中心',
      meta: { alwaysShow: true, title: '日志中心',  },
      children: [
        {
          path: '/rizhi',
          component: () => import('../views/09-rizhi/rizhi.vue'),
          name: 'rizhi',
          meta: {icon: 'rizhi', title: '用户日志' }
        }
      ]
    },
    {
      path: '/juanti',
      component: layout,
      redirect: '/shijuan',
      meta: { icon: "用户", title: "卷题管理" }, // 文档/试卷图标
      children: [
        {
          path: '/shijuanlist',
          component: () => import('../views/juantiManage/ShijuanList.vue'),
          meta: { title: "试卷列表" }
        },
        {
          path: '/timulist',
          component: () => import('../views/juantiManage/TimuList.vue'),
          meta: { title: "题目列表" }
        },
        {
          path: '/shijuanadd',
          component: () => import('../views/juantiManage/Addshijuan.vue'),
          meta: { title: "试卷添加", hidden: "true" }
        },
        // 修改为带ID参数的编辑路由
        {
          path: '/shijuanedit/:id',  
          component: () => import('../views/juantiManage/Editshijuan.vue'),
          meta: { title: "试卷编辑", hidden: "true" }
        },
        {
          path: '/danxuan',  
          component: () => import('../views/juantiManage/1.vue'),
          meta: { title: "单选", hidden: "true" }
        },
        {
          path: '/duoxuan',  
          component: () => import('../views/juantiManage/2.vue'),
          meta: { title: "多选", hidden: "true" }
        },
        {
          path: '/panduan', 
          component: () => import('../views/juantiManage/3.vue'),
          meta: { title: "判断", hidden: "true" }
        },
        {
          path: '/tiankong',  
          component: () => import('../views/juantiManage/4.vue'),
          meta: { title: "填空", hidden: "true" }
        },
        {
          path: '/jieda',  
          component: () => import('../views/juantiManage/5.vue'),
          meta: { title: "解答", hidden: "true" }
        },
      ]
    },
    {
      path: '/renwu',
      component: layout,
      redirect: '/renwuList',
      meta: { icon: "用户", title: "任务管理" }, // 列表/任务图标
      children: [
        {
          path: '/renwulist',
          component: () => import('../views/renwuManage/RenwuList.vue'),
          meta: { title: "任务列表" }
        },
        {
          path: '/renwucreate',
          component: () => import('../views/renwuManage/RenwuCreate.vue'),
          meta: { title: "任务创建" }
        },
        {
          path: '/renwuedit',
          component: () => import('../views/renwuManage/RenwuEdit.vue'),
          meta: { title: "任务编辑", hidden: "true"}
        },
      ]
    },
    {
      path: '/jiaoyu',
      component: layout,
      redirect: '/xueke/xuekelist',
      meta: { icon: "lock", title: "教育管理" }, // 笔记本/学科图标
      children: [
        {
          path: '/xuekelist',
          component: () => import('../views/jiaoyuManage/XuekeList.vue'),
          meta: { title: "学科列表" }
        },
        {
          path: '/xuekeadd',
          component: () => import('../views/jiaoyuManage/XuekeAdd.vue'),
          meta: { title: "学科添加", hidden: "true" }
        },
        {
          path: '/xuekeedit/:id?',  // 添加可选ID参数
          component: () => import('../views/jiaoyuManage/XuekeEdit.vue'),
          meta: { title: "学科编辑", hidden: "true" }
        }
      ]
    }
  ],
})

router.beforeEach((to, form, next) => {
  const token = Cookies.get('JSESSIONID')
  if (token) {
    if (to.path == "/login") {
      next("/")
    } else {
      next()
    }
  } else {
    if (to.path == "/login") {
      next()
    } else {
      next({ path: "/login" })
    }
  }
})

export default router